package com.tan.steamship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.domain.CommentPo;
import com.tan.steamship.domain.OrdersPo;
import com.tan.steamship.domain.RoutesPo;
import com.tan.steamship.dto.RoutesDto;
import com.tan.steamship.mapper.CommentMapper;
import com.tan.steamship.mapper.RoutesMapper;
import com.tan.steamship.service.CommentService;
import com.tan.steamship.service.OrdersService;
import com.tan.steamship.service.RoutesService;
import com.tan.steamship.vo.CommentVO;
import com.tan.steamship.vo.RoutesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 85726
* @description 针对表【routes(航线信息表)】的数据库操作Service实现
* @createDate 2024-12-10 22:13:09
*/
@Service
public class RoutesServiceImpl extends ServiceImpl<RoutesMapper, RoutesPo>
    implements RoutesService{

    @Autowired
    CommentService commentService;
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    OrdersService ordersService;

    @Override
    public Page<RoutesVo> listRoutes(Integer type, int page, int size) {
        Page<RoutesVo> result = new Page<>();
        List<RoutesVo> listVo = new ArrayList<>();
        LambdaQueryWrapper<RoutesPo> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(type) && 0 != type) {
            queryWrapper.eq(RoutesPo::getType, type);
        }
        if (ObjectUtil.isNotEmpty(type) && 0 == type) {
            queryWrapper.orderByDesc(RoutesPo::getViews);
        } else {
            queryWrapper.orderByDesc(RoutesPo::getCreateTime);
        }
        Page<RoutesPo> routesPage = page(new Page<>(page, size), queryWrapper);
        List<RoutesPo> poList = routesPage.getRecords();
        // 销量榜前10的航线
        if (ObjectUtil.isNotEmpty(type) && 8 == type) {
            poList = getSales();
        }
        if (CollUtil.isNotEmpty(poList)) {
            listVo = BeanUtil.copyToList(poList, RoutesVo.class, new CopyOptions());
        }
        result.setTotal(routesPage.getTotal());
        result.setCurrent(routesPage.getCurrent());
        result.setRecords(listVo);
        return result;
    }

    public List<RoutesPo> getSales() {
        List<RoutesPo> routesPos = new LinkedList<>();
        // 查询订单表取前10的routeid
        LambdaQueryWrapper<OrdersPo> ordersPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersPoLambdaQueryWrapper.select(OrdersPo::getRouteId)
                .groupBy(OrdersPo::getRouteId)
                .orderByDesc(OrdersPo::getRouteId)
                .last("limit 10");
        List<OrdersPo> ordersPos = ordersService.list(ordersPoLambdaQueryWrapper);
        if (CollUtil.isNotEmpty(ordersPos)) {
            List<Integer> routeIds = ordersPos.stream().map(OrdersPo::getRouteId).collect(Collectors.toList());
            routesPos = this.listByIds(routeIds);
        }
        return routesPos;
    }

    @Override
    public void saveOrUpdate(RoutesDto routesDto) {
        RoutesPo routesPo = BeanUtil.copyProperties(routesDto, RoutesPo.class);
        routesPo.setUpdateTime(new Date());
        this.saveOrUpdate(routesPo);
    }

    @Override
    public RoutesVo detail(String id) {
        RoutesVo result = new RoutesVo();
        RoutesPo routesPo = this.getById(id);
        if (ObjectUtil.isEmpty(routesPo)) {
            return result;
        }
        // 每浏览一次，浏览量加1
        routesPo.setViews(routesPo.getViews() + 1);
        this.updateById(routesPo);
        // 评论
        LambdaQueryWrapper<CommentPo> commentPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentPoLambdaQueryWrapper.eq(CommentPo::getRouteId, id);
        List<CommentPo> commentPos = commentService.list(commentPoLambdaQueryWrapper);
        List<CommentVO> commentVOList = new LinkedList<>();
        if (CollUtil.isNotEmpty(commentPos)) {
            commentPos.forEach(c->{
                CommentVO vo = new CommentVO();
                BeanUtil.copyProperties(c, vo);
                vo.setParentComment(commentService.getById(vo.getParentCommentId()));
                commentVOList.add(vo);
            });
        }
        result = BeanUtil.copyProperties(routesPo, RoutesVo.class);
        result.setComments(commentVOList);
        return result;
    }
}




